function ajax(url) {
  return new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();
    xhr.open("GET", url);
    xhr.responseType = "json";
    xhr.onload = function () {
      if (this.status === 200) {
        resolve(this.response);
      } else {
        reject(this.statusText);
      }
    };
    xhr.send();
  });
}

function* main() {
  try {
    const user = yield ajax("./api/users.json");
    console.log(user, "main user.json");

    const post = yield ajax("./api/posts.json");
    console.log(post, "main post.json");
  } catch (error) {
    console.log(error);
  }
}

function co(generator) {
  const g = generator();

  function handleResult(result) {
    if (result.done) return;
    result.value.then(
      (data) => {
        handleResult(g.next(data));
      },
      (err) => g.throw(err)
    );
  }

  handleResult(g.next());
}

co(main);
